const loginName=window.sessionStorage.getItem("loginName")
const numReg=/^([1-9]{1}[0-9]{0,7})$/
new Vue({
    el:'#allBox',
    data:{
        name:'',
        imgStr:'http://192.168.2.106:8848/images/',
        car:{
        },
        totalPrice:0,
        details:[],
        infoFlag:false,
        infoText:'',
        detailList:[]
    },
    methods:{
        getCarFromRedis()
        {
            axios.get("/api/car/getCar",{
                params:{
                    loginName
                }
            }).then(rs=>{
                this.car=rs.data.car
                console.log(this.car)
            })
        },
        checkNum(id)
        {

            if(isNaN(this.car.carDetailMap[id].num))
            {
                this.car.carDetailMap[id].num=1
            }
            if(!numReg.test(this.car.carDetailMap[id].num))
            {
                this.car.carDetailMap[id].num=1
            }
            this.checkTest(id)
        },
        addNum(id)
        {
            this.car.carDetailMap[id].num++
            this.checkTest(id)
        },
        deNum(id)
        {
            this.car.carDetailMap[id].num--
            this.checkTest(id)
        },
        checkTest(id)
        {
            if(this.car.carDetailMap[id].num<=0)
            {
                this.car.carDetailMap[id].num=1
            }
            if(this.car.carDetailMap[id].num>=this.car.carDetailMap[id].stock)
            {
                this.car.carDetailMap[id].num=this.car.carDetailMap[id].stock
            }
            this.car.carDetailMap[id].numPrice= this.car.carDetailMap[id].num* this.car.carDetailMap[id].price
            this.getTotal()
        },
        getTotal()
        {
            this.totalPrice=0
            this.details.forEach((value,index)=>{
                this.totalPrice+=this.car.carDetailMap[value].numPrice
            })
        },

        //删除
        deleteDetail(id)
        {
            if(confirm("确认删除该条商品吗"))
            {
                axios.get("/api/car/deleteDetail",{
                    params:{
                        id,
                        loginName
                    }
                }).then(rs=>{
                    if(rs.data.isDelete==="yes")
                    {
                        this.infoText="删除成功"
                        this.infoFlag=true
                        setTimeout(()=>{
                            this.infoFlag=false
                        },700)
                        this.getCarFromRedis()
                    }else{
                        alert("删除出错了")
                    }
                })
            }
        },
        getBill()
        {
            if(this.details.length===0)
            {
                this.infoText="请先勾选商品"
                this.infoFlag=true
                setTimeout(()=>{
                    this.infoFlag=false
                },700)
                return
            }
            //获取选中的商品
            this.details.forEach((value,index)=>{
                this.detailList.push(this.car.carDetailMap[value])
            })
            const formData=new FormData()
            formData.append("detailStr",JSON.stringify(this.detailList))
            formData.append("loginName",loginName)
            //更新redis中的数据
            axios.post("/api/car/updateCarFromRedis",
                formData
                ).then(rs=>{
                    if(rs.data.isUpdate==="yes")
                    {
                        window.localStorage.setItem("detailList",JSON.stringify(this.detailList))
                        window.location="/api/BuyCar_Two.html"
                    }
            })

        }
    },
    created() {
        this.getCarFromRedis()
    }
})